package com.jddup.program.dao.sqlbuilder.voltdb;

import com.jddup.common.pojo.TableColumn;
import com.jddup.common.pojo.TableMeta;
import com.jddup.program.dao.sqlbuilder.InsertPrepareSQLBuilder;

import java.util.List;

/**
 * voltdb插入sql构建器
 * Created by ZZB on 2019/03/06.
 */
public class VoltdbInsertPrepareSQLBuilder implements InsertPrepareSQLBuilder {
	private final static String INSERT_SQL = "insert into {tableName} ({columns}) values ({questionMarks});";

	@Override
	public String buildInsertSQL(TableMeta tableMeta) {
		String tableName = tableMeta.getTableName();
		List<TableColumn> tableColumns = tableMeta.getTableColumns();
		if (tableColumns.size() == 0) {
			return "";
		}
		String columns = tableColumns.stream().map(TableColumn::getColumnName)
				.reduce((s, s2) -> s + "," + s2).get();
		String questionMarks = tableColumns.stream().map(tableColumn -> "?").reduce((s, s2) -> s + "," + s2).get();
		return INSERT_SQL.replace("{tableName}", tableName).replace("{columns}", columns).replace("{questionMarks}",
				questionMarks);
	}

}
